package com.yuchengtech.simpleServer.servlet;

import java.io.PrintWriter;
import net.sf.json.JSONObject;
import com.wox.action.JsonData;
import com.yuchengtech.simpleServer.core.Constant;
import com.yuchengtech.simpleServer.core.Http;
import com.yuchengtech.simpleServer.core.HttpRequest;
import com.yuchengtech.simpleServer.core.HttpResponse;
import com.yuchengtech.simpleServer.core.exception.SimpleServerException;

public class JsonServlet extends SimpleServlet{

	public static final String RES_WAY="resWay";
	public static final String BY_JS="js";
	public static final String BY_JAVA="java";
	
	
	@Override
	public void destory() throws SimpleServerException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void init() throws SimpleServerException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void service(HttpRequest request, HttpResponse response)
			throws SimpleServerException {
		
		JsonData jsonData=(JsonData) request.getAttribute(Constant.JSONDATA_REQUEST);
		PrintWriter writer=response.getPrintWriter();
		String resWay=request.getParameter(RES_WAY);
		if(resWay==null)
			resWay=BY_JS;
	
				response.setMimeType(Http.MIME_TEXT);
				response.setCharset("utf-8");
				JSONObject jsonObj=JSONObject.fromObject(jsonData);
				String jsonStr=jsonObj.toString();
				writer.print("("+jsonStr+")");
			/*
				if(BY_JS.equals(resWay))
			{
			}
		else
		{
			System.out.println("正在开发");
			//需要通过Server端来控制
		}*/
	}

}
